﻿#include <QFileDialog>
#include <QListWidgetItem>
#include <QMessageBox>
#include <QMenu>
#include <QDir>
#include <QSaveFile>
#include <QTreeWidgetItem>
#include "GlobalData.h"
#include "PageDataJudge.h"
#include "ui_PageDataJudge.h"
#include "../DatabaseMgmt/Country.h"
#include "../DatabaseMgmt/TestReport.h"
#include "../DatabaseMgmt/AccordLaw.h"
#include "../DatabaseMgmt/ResultJudge.h"
#include "../DatabaseMgmt/SampleProperty.h"
#include "../DatabaseMgmt/ExteriorTest.h"
#include "../DatabaseMgmt/GoodsStandard.h"
#include "../DatabaseMgmt/PhysicochemicalAnalyse.h"
#include "../DatabaseMgmt/ElementValue.h"
#include "XmlFileReader.h"
#include "ImageReader.h"



CPageDataJudge::CPageDataJudge(QWidget* parent /*= 0*/, Qt::WindowFlags flags /*= 0*/)
	: QWidget(parent, flags)
	, m_ui(nullptr)
{
	m_nIdentityStart = 100;
	init();
}

CPageDataJudge::~CPageDataJudge()
{

}
// 打开报告
void CPageDataJudge::openTestReport()
{
	// 打开首页
	openReportHomeInfo();
	// 检验鉴别依据
	m_ui->wgtLaw->openTestReport();
	// 样品性状
	m_ui->wgtSample->openTestReport();
	// 检测分析
	m_ui->wgtAnalyse->openTestReport();
	// 货物结果判别
	m_ui->wgtJudge->openTestReport();
}

void CPageDataJudge::init()
{
	m_ui = new Ui::PageDataJudge();
	m_ui->setupUi(this);

	initData();

	showInitTabWidget(false);
	initTestReportLiet();


	connect(m_ui->twSampleReport, SIGNAL(customContextMenuRequested(const QPoint&)),
		this, SLOT(slotSampleReportRightMenu(const QPoint&)));
	connect(m_ui->twSampleReport, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)),
		this, SLOT(slotTreeDoubleClicked(QTreeWidgetItem*, int)));
	connect(m_ui->btnReportSave, SIGNAL(clicked()), this, SLOT(slotReportSave()));
	connect(m_ui->btnHome, SIGNAL(clicked()), this, SIGNAL(sigHome()));
	connect(m_ui->btnExport, SIGNAL(clicked()), this, SLOT(slotExportClicked()));
}

void CPageDataJudge::initData()
{
	initCountryList();

	initTestReportLiet();
}
// 清空报告相关页面数据
void CPageDataJudge::clear()
{
	clearHomePageData();

	clearNotFirstPageData();
}
// 清空非首页数据
void CPageDataJudge::clearNotFirstPageData()
{
	// 检验鉴别依据
	m_ui->wgtLaw->clear();
	// 样品性状
	m_ui->wgtSample->clear();
	// 检测分析
	m_ui->wgtAnalyse->clear();
	// 货物结果判别
	m_ui->wgtJudge->clear();
}

void CPageDataJudge::clearHomePageData()
{
	m_ui->leReportName->clear();
	m_ui->leReportCode->clear();
	m_ui->deAcceptDate->setDate(QDate(2020, 1, 1));
	m_ui->leSampeCode->clear();
	m_ui->leClientor->clear();
	m_ui->leDeclaration->clear();
	m_ui->leGoodsName->clear();
	m_ui->cmbCountry->setCurrentIndex(0);
	m_ui->cmbManner->setCurrentIndex(0);
	m_ui->leBillEntry->clear();
	m_ui->deAcceptDate->setDate(QDate(2020, 1, 1));
	m_ui->spbSampleCount->clear();
	m_ui->leSealNo->clear();
}

void CPageDataJudge::initCountryList()
{
	CCountry country;
	
	QVector<CountryInfo> countries;
	int ret = country.queryAll(countries);	
	if (CONST_ERR_SUCCESS != ret)
	{
		return;
	}
	m_ui->cmbCountry->clear();
	//m_ui->cmbOutputCountry->clear();

	for (CountryInfo info : countries)
	{
		m_ui->cmbCountry->addItem(info.name, info.uid);
		//m_ui->cmbOutputCountry->addItem(info.name, info.uid);
	}
	m_ui->cmbCountry->setCurrentIndex(0);
}

void CPageDataJudge::initTestReportLiet()
{
	m_ui->twSampleReport->clear();

	CTestReport report;
	QVector<TestReport> vectReport;
	report.queryAll(vectReport);

	for (int i = 0; i < vectReport.size(); ++i)
	{
		QTreeWidgetItem* pItem = new QTreeWidgetItem(m_ui->twSampleReport);
		pItem->setText(0, vectReport.at(i).name);
		pItem->setData(0, Qt::UserRole, vectReport.at(i).uid);

	}
}

void CPageDataJudge::showInitTabWidget(bool enable)
{
	m_ui->tabWidget->setTabEnabled(0, true);
	m_ui->tabWidget->setTabEnabled(1, enable);
	m_ui->tabWidget->setTabEnabled(2, enable);
	m_ui->tabWidget->setTabEnabled(3, enable);
	m_ui->tabWidget->setTabEnabled(4, enable);
	m_ui->tabWidget->setTabEnabled(5, enable);

	m_ui->tabWidget->setCurrentIndex(0);
}

void CPageDataJudge::enableReportTabWidget()
{
	m_ui->tabWidget->setTabEnabled(0, true);
	m_ui->tabWidget->setTabEnabled(1, true);
	m_ui->tabWidget->setTabEnabled(2, true);
	m_ui->tabWidget->setTabEnabled(3, true);
	m_ui->tabWidget->setTabEnabled(4, true);
	m_ui->tabWidget->setTabEnabled(5, true);
}

// 打开报告首页
void CPageDataJudge::openReportHomeInfo()
{
	uint32_t nTestReportId = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportId == 0)
	{
		return;
	}

	CTestReport report;
	TestReport data;
	int ret = report.queryById(nTestReportId, data);
	if (CONST_ERR_SUCCESS != ret)
	{
		qInfo() << QStringLiteral("打开检测报告失败：") << ret;
		return;
	}

	m_ui->leReportName->setText(data.name);
	m_ui->leReportCode->setText(data.report_code);
	m_ui->deAcceptDate->setDate(data.report_date);
	m_ui->leSampeCode->setText(data.sample_code);
	m_ui->leClientor->setText(data.clientor);
	m_ui->leDeclaration->setText(data.declaration_name);;
	m_ui->leGoodsName->setText(data.goods_name);
	m_ui->cmbCountry->setCurrentText(data.from_region);
	m_ui->cmbManner->setCurrentText(data.sample_way);
	m_ui->leBillEntry->setText(data.bill_entry);
	m_ui->deAcceptDate->setDate(data.sample_date);
	m_ui->spbSampleCount->setValue(data.sample_count);
	m_ui->leSealNo->setText(data.sample_seal_no);
	m_ui->deBegin->setDate(data.begin_date);
	m_ui->deEnd->setDate(data.end_date);
}
// 保存首页
void CPageDataJudge::saveReportHomeInfo()
{
	TestReport row;

	row.name = m_ui->leReportName->text().trimmed();
	row.report_code = m_ui->leReportCode->text().trimmed();
	row.report_date = m_ui->deReportDate->date();
	row.sample_code = m_ui->leSampeCode->text().trimmed();
	row.clientor = m_ui->leClientor->text().trimmed();
	row.declaration_name = m_ui->leDeclaration->text().trimmed();
	row.goods_name = m_ui->leGoodsName->text().trimmed();
	row.from_region = m_ui->cmbCountry->currentText();
	row.sample_way = m_ui->cmbManner->currentText();
	row.bill_entry = m_ui->leBillEntry->text().trimmed();
	row.sample_date = m_ui->deAcceptDate->date();
	row.sample_count = m_ui->spbSampleCount->value();
	row.sample_seal_no = m_ui->leSealNo->text().trimmed();

	CTestReport report;

	if (CGlobalData::getInstance()->getTestReportUid() != 0) // 修改
	{
		row.uid = CGlobalData::getInstance()->getTestReportUid();
		int ret = report.modifyRow(row);
		if (ret != CONST_ERR_SUCCESS)
		{
			qInfo() << QStringLiteral("更新数据失败，请检查数据：") << ret;
		}
	}
	else // 添加
	{
		uint32_t uid = 0;
		int ret = report.insertRow(row, uid);
		if ( ret != CONST_ERR_SUCCESS)
		{
			qInfo() << QStringLiteral("插入数据失败，请检查数据：") << ret;
			return;
		}
		QTreeWidgetItem* pItem = new QTreeWidgetItem();
		pItem->setText(0, row.name);
		pItem->setData(0, Qt::UserRole, uid);
		m_ui->twSampleReport->addTopLevelItem(pItem);

		enableReportTabWidget();

		CGlobalData::getInstance()->setTestReportUid(uid);

		clearNotFirstPageData();
	}
}

void CPageDataJudge::slotSampleReportRightMenu(const QPoint& pos)
{
	Q_UNUSED(pos);
	QMenu menu(this);

	menu.addAction(QStringLiteral("打开报告"), this, SLOT(slotOpenReportTriggered(bool)));
	menu.addAction(QStringLiteral("保存报告"), this, SLOT(slotSaveReportTriggered(bool)));
	menu.addAction(QStringLiteral("删除报告"), this, SLOT(slotDeleteReportTriggered(bool)));
	menu.addAction(QStringLiteral("导出报告"), this, SLOT(slotExportReportTriggered(bool)));

	menu.exec(QCursor::pos());
}

void CPageDataJudge::slotOpenReportTriggered(bool checked /*= false*/)
{
	Q_UNUSED(checked);
	QTreeWidgetItem* pItem = m_ui->twSampleReport->currentItem();

	if (!pItem)
	{
		return;
	}

	showInitTabWidget(true);

	uint32_t uid = pItem->data(0, Qt::UserRole).toUInt();
	CGlobalData::getInstance()->setTestReportUid(uid);
	
	openTestReport();
}

void CPageDataJudge::slotSaveReportTriggered(bool checked /*= false*/)
{
	Q_UNUSED(checked);
	// 保存首页
	saveReportHomeInfo();
	// 检验鉴别依据
	m_ui->wgtLaw->savePage();
	// 样品性状
	m_ui->wgtSample->savePage();
	// 检测分析
	m_ui->wgtAnalyse->savePage();
	// 货物结果判别
	m_ui->wgtJudge->savePage();

}

void CPageDataJudge::slotDeleteReportTriggered(bool checked /*= false*/)
{
	Q_UNUSED(checked);
	QTreeWidgetItem* pItem = m_ui->twSampleReport->currentItem();

	if (!pItem)
	{
		return;
	}
	uint32_t uid = pItem->data(0, Qt::UserRole).toUInt();
	CTestReport report;
	if (report.removeRow(uid) != CONST_ERR_SUCCESS)
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("删除报告失败！"));
		return;
	}

	m_ui->twSampleReport->takeTopLevelItem(m_ui->twSampleReport->indexOfTopLevelItem(pItem));
	delete pItem;
	pItem = nullptr;

	CGlobalData::getInstance()->clear();
	clear();
}

// 导出报告
void CPageDataJudge::slotExportReportTriggered(bool checked /*= false*/)
{
	Q_UNUSED(checked);
	QTreeWidgetItem* pItem = m_ui->twSampleReport->currentItem();

	if (!pItem)
	{
		return;
	}
	uint32_t uid = pItem->data(0, Qt::UserRole).toUInt();
	exportReportFile(uid);

}
// 双击打开报告
void CPageDataJudge::slotTreeDoubleClicked(QTreeWidgetItem* pItem, int column)
{
	Q_UNUSED(column);
	if (nullptr == pItem)
	{
		return;
	}
	uint32_t uid = pItem->data(0, Qt::UserRole).toUInt();
	CGlobalData::getInstance()->setTestReportUid(uid);

	showInitTabWidget(true);

	openTestReport();
}

void CPageDataJudge::slotReportSave()
{
	TestReport row;

	row.name = m_ui->leReportName->text().trimmed();
	row.report_code = m_ui->leReportCode->text().trimmed();
	row.report_date = m_ui->deAcceptDate->date();
	row.sample_code = m_ui->leSampeCode->text().trimmed();
	row.clientor = m_ui->leClientor->text().trimmed();
	row.declaration_name = m_ui->leDeclaration->text().trimmed();
	row.goods_name = m_ui->leGoodsName->text().trimmed();
	row.from_region = m_ui->cmbCountry->currentText();
	row.sample_way = m_ui->cmbManner->currentText();
	row.bill_entry = m_ui->leBillEntry->text().trimmed();
	row.sample_date = m_ui->deAcceptDate->date();
	row.sample_count = m_ui->spbSampleCount->value();
	row.sample_seal_no = m_ui->leSealNo->text().trimmed();
	row.begin_date = m_ui->deBegin->date();
	row.end_date = m_ui->deEnd->date();

	CTestReport report;

	if (CGlobalData::getInstance()->getTestReportUid() != 0) // 修改
	{
		row.uid = CGlobalData::getInstance()->getTestReportUid();

		if (report.modifyRow(row) != CONST_ERR_SUCCESS)
		{
			QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("更新数据失败，请检查数据！"));
		}
	}
	else // 添加
	{
		uint32_t uid = 0;
		if (report.insertRow(row, uid) != CONST_ERR_SUCCESS)
		{
			QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("插入数据失败，请检查数据！"));
			return;
		}
		QTreeWidgetItem* pItem = new QTreeWidgetItem();
		pItem->setText(0, row.name);
		pItem->setData(0, Qt::UserRole, uid);
		m_ui->twSampleReport->addTopLevelItem(pItem);

		enableReportTabWidget();

		CGlobalData::getInstance()->setTestReportUid(uid);

		clearNotFirstPageData();
	}
}
// 按钮导出
void CPageDataJudge::slotExportClicked()
{
	uint32_t nTestReportId = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportId == 0)
	{
		return;
	}

	exportReportFile(nTestReportId);
}

void CPageDataJudge::exportReportFile(uint32_t nTestReportId)
{
	if (nTestReportId == 0)
	{
		return;
	}
	TestReport data;
	CTestReport report;
	int ret = report.queryById(nTestReportId, data);

	if (CONST_ERR_SUCCESS != ret)
	{
		return;
	}
	QString strHomeDir = QDir::home().absolutePath();

	strHomeDir = QFileDialog::getExistingDirectory(this, QStringLiteral("导出目录"), strHomeDir);

	if (strHomeDir.isEmpty())
	{
		return;
	}
	QString strOriginalFile = strHomeDir + "/original_" + data.name + ".docx";
	QString strCopyFile = strHomeDir + "/copy_" + data.name + ".docx";

	// 填充正本数据
	QByteArray originalXml = CXmlFileReader::readOriginal();
	fillWordData(nTestReportId, originalXml);

	// 填充副本数据
	QByteArray copyXml = CXmlFileReader::readCopy();
	fillWordData(nTestReportId, copyXml);
	
	// 保存文件
	// 保存正本
	QSaveFile originalFile(strOriginalFile);
	originalFile.open(QIODevice::WriteOnly);
	int len = originalFile.write(originalXml);
	ret = originalFile.commit();
	// 保存副本
	QSaveFile copyFile(strCopyFile);
	copyFile.open(QIODevice::WriteOnly);
	len = copyFile.write(copyXml);
	ret = copyFile.commit();
}

void CPageDataJudge::fillWordData(uint32_t nTestReportId, QByteArray& word)
{
	if (word.length() > CONST_MAX_SIZE)
	{
		qCritical() << QStringLiteral("不支持超大文件！");
		return;
	}
	// 填充报告
	fillReportData(nTestReportId, word);
	// 填充法律依据数据
	fileLawData(nTestReportId, word);
	// 填充样品性状
	fileSamplePropertyData(nTestReportId, word);
	// 填充检测分析
	fileTestAnalyseData(nTestReportId, word);
	// 填充判别结果
	fileResultData(nTestReportId, word);
}

void CPageDataJudge::fillReportData(uint32_t nTestReportId, QByteArray &word)
{
	TestReport data;
	CTestReport report;
	report.queryById(nTestReportId, data);

	// 页眉报告编号索引
	word.replace(CONST_PAGE_HEADER_REPORT_CODE_IDX.toLocal8Bit(), data.report_code.toLocal8Bit());
	// 页眉年索引
	word.replace(CONST_PAGE_HEADER_YEAR_IDX.toLocal8Bit(), QString::number(data.report_date.year()).toLocal8Bit());
	// 页眉月索引
	word.replace(CONST_PAGE_HEADER_MOUTH_IDX.toLocal8Bit(), QString::number(data.report_date.month()).toLocal8Bit());
	// 页眉日索引
	word.replace(CONST_PAGE_HEADER_DAY_IDX.toLocal8Bit(), QString::number(data.report_date.day()).toLocal8Bit());
	// 委托方索引
	word.replace(CONST_DOCUMENT_CLIENTOR_IDX.toLocal8Bit(), data.clientor.toLocal8Bit());
	// 申报名称
	word.replace(CONST_DOCUMENT_DECLARATION_NAME_IDX.toLocal8Bit(), data.declaration_name.toLocal8Bit());
	// 来源地
	word.replace(CONST_DOCUMENT_FROM_WHREE_IDX.toLocal8Bit(), data.from_region.toLocal8Bit());
	// 报关单号
	word.replace(CONST_DOCUMENT_DECLARATION_CODE_IDX.toLocal8Bit(), data.bill_entry.toLocal8Bit());
	// 样品签封号
	word.replace(CONST_DOCUMENT_SAMPLE_SEAL_CODE_IDX.toLocal8Bit(), data.sample_seal_no.toLocal8Bit());
	// 来样方式
	word.replace(CONST_DOCUMENT_SAMPLE_WAY_IDX.toLocal8Bit(), data.sample_way.toLocal8Bit());
	// 来样数量
	word.replace(CONST_DOCUMENT_SAMPLE_COUNT_IDX.toLocal8Bit(), QString::number(data.sample_count).toLocal8Bit());
	// 接样日期-年
	word.replace(CONST_DOCUMENT_SAMPLE_ACCEPT_YEAR_IDX.toLocal8Bit(), QString::number(data.sample_date.year()).toLocal8Bit());
	// 接样日期-月
	word.replace(CONST_DOCUMENT_SAMPLE_ACCEPT_MOUTH_IDX.toLocal8Bit(), QString::number(data.sample_date.month()).toLocal8Bit());
	// 接样日期-日
	word.replace(CONST_DOCUMENT_SAMPLE_ACCEPT_DAY_IDX.toLocal8Bit(), QString::number(data.sample_date.day()).toLocal8Bit());
	// 检验日期起始-年
	word.replace(CONST_DOCUMENT_SAMPLE_TEST_BEGIN_YEAR_IDX.toLocal8Bit(), QString::number(data.begin_date.year()).toLocal8Bit());
	// 检验日期起始-月
	word.replace(CONST_DOCUMENT_SAMPLE_TEST_BEGIN_MOUTH_IDX.toLocal8Bit(), QString::number(data.begin_date.month()).toLocal8Bit());
	// 检验日期起始-日
	word.replace(CONST_DOCUMENT_SAMPLE_TEST_BEGIN_DAY_IDX.toLocal8Bit(), QString::number(data.begin_date.day()).toLocal8Bit());
	if (data.begin_date.year() != data.end_date.year())
	{
		QString strText = QStringLiteral("%1年%2")
			.arg(data.end_date.year())
			.arg(data.end_date.month());
		word.replace(CONST_DOCUMENT_SAMPLE_TEST_END_MOUTH_IDX.toLocal8Bit(), strText.toLocal8Bit());
	}
	else
	{
		// 检验日期结束-月
		word.replace(CONST_DOCUMENT_SAMPLE_TEST_END_MOUTH_IDX.toLocal8Bit(), QString::number(data.end_date.month()).toLocal8Bit());
	}
	// 检验日期结束-日
	word.replace(CONST_DOCUMENT_SAMPLE_TEST_END_DAY_IDX.toLocal8Bit(), QString::number(data.end_date.day()).toLocal8Bit());
}

// 填充法律依据数据
void CPageDataJudge::fileLawData(uint32_t nTestReportId, QByteArray &word)
{
	CAccordLaw accord;
	QVector<ViewAccordLaw> lawes;
	accord.queryByReportUid(nTestReportId, lawes);

	QString strText;

	for (int i = 0; i < lawes.size(); ++i)
	{
		ViewAccordLaw row = lawes.at(i);

		strText += row.appraisal_name;
		strText += "\n";
	}
	// 检验鉴别依据
	word.replace(CONST_DOCUMENT_LAW_IDX.toLocal8Bit(), strText.toLocal8Bit());
}
// 填充样品性状
void CPageDataJudge::fileSamplePropertyData(uint32_t nTestReportId, QByteArray &word)
{
	CSampleProperty property;
	SampleProperty data;
	property.queryByReportUid(nTestReportId, data);

	QString strText = data.pack_describe + data.exterior_describe;
	// 样品性状描述(包装和外观)
	word.replace(CONST_DOCUMENT_SAMPLE_DESCRIBE_IDX.toLocal8Bit(), strText.toLocal8Bit());

	// 包装图
	QString strPackImage = QString("image%1.png")
		.arg(m_nIdentityStart);
	QByteArray imagePackXmlData = CXmlFileReader::readImageData();
	QByteArray imagePackData = CImageReader::readImageBase64(data.pack_image);

	imagePackXmlData.replace(QByteArray("%001"), strPackImage.toLocal8Bit());
	imagePackXmlData.replace(QByteArray("%002"), imagePackData);

	QByteArray packIdentity = CXmlFileReader::readImageIdentity();
	QString strPackId = QString("rId%1")
		.arg(m_nIdentityStart);
	packIdentity.replace(QByteArray("%001"), strPackId.toLocal8Bit());
	packIdentity.replace(QByteArray("%002"), strPackImage.toLocal8Bit());

	QByteArray packImage = CXmlFileReader::readImage();

	packImage.replace(QByteArray("%001"), QByteArray("1"));
	packImage.replace(QByteArray("%002"), QByteArray("photo"));
	packImage.replace(QByteArray("%003"), QByteArray("describe"));
	packImage.replace(QByteArray("%004"), QByteArray("1"));
	packImage.replace(QByteArray("%005"), QByteArray("photo"));
	packImage.replace(QByteArray("%006"), QByteArray("describe"));
	packImage.replace(QByteArray("%007"), strPackId.toLocal8Bit());

	m_nIdentityStart++;
	// 外观图
	QString strExteriorImage = QString("image%1.png")
		.arg(m_nIdentityStart);
	QByteArray imageExteriorXmlData = CXmlFileReader::readImageData();
	QByteArray imageExteriorData = CImageReader::readImageBase64(data.exterior_image);

	imageExteriorXmlData.replace(QByteArray("%001"), strExteriorImage.toLocal8Bit());
	imageExteriorXmlData.replace(QByteArray("%002"), imageExteriorData);

	QByteArray exteriorIdentity = CXmlFileReader::readImageIdentity();
	QString strExteriorId = QString("rId%1")
		.arg(m_nIdentityStart);
	exteriorIdentity.replace(QByteArray("%001"), strExteriorId.toLocal8Bit());
	exteriorIdentity.replace(QByteArray("%002"), strExteriorImage.toLocal8Bit());

	QByteArray exteriorImage = CXmlFileReader::readImage();

	exteriorImage.replace(QByteArray("%001"), QByteArray("2"));
	exteriorImage.replace(QByteArray("%002"), QByteArray("photo"));
	exteriorImage.replace(QByteArray("%003"), QByteArray("describe"));
	exteriorImage.replace(QByteArray("%004"), QByteArray("2"));
	exteriorImage.replace(QByteArray("%005"), QByteArray("photo"));
	exteriorImage.replace(QByteArray("%006"), QByteArray("describe"));
	exteriorImage.replace(QByteArray("%007"), strExteriorId.toLocal8Bit());

	QByteArray imageIdentity = packIdentity + exteriorIdentity;
	word.replace(CONST_DOCUMENT_WORD_IMAGE_IDENTITY_IDX, imageIdentity);

	QByteArray imageData = imagePackXmlData + imageExteriorXmlData;
	word.replace(CONST_DOCUMENT_WORD_IMAGE_DATA_IDX, imageData);
	
	word.replace(CONST_DOCUMENT_SAMPLE_PACK_IMAGE_IDX, packImage);
	word.replace(CONST_DOCUMENT_SAMPLE_EXTERIOR_IMAGE_IDX, exteriorImage);
}
// 填充检测分析
void CPageDataJudge::fileTestAnalyseData(uint32_t nTestReportId, QByteArray &word)
{
	CExteriorTest exterior;
	ExteriorTest exteriorData;
	exterior.queryByReportUid(nTestReportId, exteriorData);

	// 外观检测
	word.replace(CONST_DOCUMENT_SAMPLE_INCLUSION_IDX, exteriorData.inclusion.toLocal8Bit());
	word.replace(CONST_DOCUMENT_SAMPLE_UNBREAKABLE_IDX, exteriorData.unbreakable.toLocal8Bit());

	// 理化分析
	CPhysicochemicalAnalyse analyse;
	PhysicochemicalAnalyse analyseData;
	analyse.queryByReportUid(nTestReportId, analyseData);
	word.replace(CONST_DOCUMENT_ELEMENT_TEST_DESCRIBE_IDX, analyseData.element_test.toLocal8Bit());
	word.replace(CONST_DOCUMENT_RADIAL_TEST_IDX, analyseData.radial_test.toLocal8Bit());

	// 元素数据表格
	// TODO
	QByteArray tableData = CXmlFileReader::readTable();
	QByteArray rowData = CXmlFileReader::readRow();
	QByteArray columnData = CXmlFileReader::readColumn();

	CElementValue viewElement;
	QVector<ElementViewValue> viewValue;
	viewElement.queryViewByReportUid(nTestReportId, viewValue);

	QByteArray elementNameColumns;
	QByteArray elementValueColuns;

	for (int i = 0; i < viewValue.size(); ++i)
	{
		QByteArray nameTemp = columnData;
		nameTemp.replace(QByteArray("%001"), viewValue.at(i).element_name.toLocal8Bit());
		elementNameColumns.append(nameTemp);

		QByteArray valueTemp = columnData;
		valueTemp.replace(QByteArray("%001"), QString::number(viewValue.at(i).element_value, 'f', 4).toLocal8Bit());
		elementValueColuns.append(valueTemp);
	}
	QByteArray elementNameRow = rowData;
	elementNameRow.replace(QByteArray("%001"), elementNameColumns);

	QByteArray elementValueRow = rowData;
	elementValueRow.replace(QByteArray("%001"), elementValueColuns);

	QByteArray elementTalbeXml = elementNameRow + elementValueRow;
	tableData.replace(QByteArray("%001"), elementTalbeXml);

	qDebug() << "table xml data:" << elementTalbeXml;

	// 表头
	if (TT_Rule == analyseData.analyse_kind)
	{
		word.replace(CONST_DOCUMENT_ELEMENT_TEST_HEADER_IDX, QStringLiteral("表1 样品元素含量").toLocal8Bit());
	}
	else if (TT_Radial == analyseData.analyse_kind)
	{
		word.replace(CONST_DOCUMENT_ELEMENT_TEST_HEADER_IDX, QStringLiteral("表1 样品元素含量（%）").toLocal8Bit());
	}
	
	// 表格
	word.replace(CONST_DOCUMENT_ELEMENT_TEST_TABLE_IDX, tableData);
	// 表格标注
	if (TT_Rule == analyseData.analyse_kind)
	{
		word.replace(CONST_DOCUMENT_ELEMENT_TEST_MARK_IDX, QStringLiteral("").toLocal8Bit());
	}
	else if (TT_Radial == analyseData.analyse_kind)
	{
		word.replace(CONST_DOCUMENT_ELEMENT_TEST_MARK_IDX, QStringLiteral("注：氧化态仅表示元素以氧化物计。").toLocal8Bit());
	}
}
// 填充判别结果
void CPageDataJudge::fileResultData(uint32_t nTestReportId, QByteArray &word)
{
	CResultJudge judge;
	ResultJudge data;
	judge.queryByReportUid(nTestReportId, data);

	// 检验鉴别依据
	word.replace(CONST_DOCUMENT_TEST_RESULT_IDX.toLocal8Bit(), data.result_describe.toLocal8Bit());
}

